package com.zjrc.meeting.controller;

import com.chinamobile.openmas.client.Mms;
import com.chinamobile.openmas.client.Sms;
import com.chinamobile.openmas.tools.MmsBuilder;
import com.chinamobile.openmas.tools.MmsConst;
import com.chinamobile.openmas.tools.MmsContent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@Controller
public class SendSMSController {

	@RequestMapping("/upload")
	public String uploadFile()throws Exception{
		
		return "second";
	}
	@RequestMapping("/tos")
	public @ResponseBody String upload( String address, String phone,HttpServletRequest request) throws UnsupportedEncodingException{
		String message=new String((request.getParameter("address")).getBytes("iso-8859-1"),"utf-8");
		String phones=new String((request.getParameter("phone")).getBytes("iso-8859-1"),"utf-8");
		System.out.println(message+"--------------"+phones);
		System.out.println("second"+"-"+address+"-"+phone);
		return "second"+"-"+message+"-"+phones;
		/*System.out.println("second"+"-"+address+"-"+phone);
		return "second"+"-"+address+"-"+phone;*/
	}
	
	/**
	 * 发送短信
	 * @param address 信息内容
	 * @param phone  号码组
	 * @param request 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/sendSMS")
	public @ResponseBody String sendSMS( String address, String phone,HttpServletRequest request) throws UnsupportedEncodingException{
		try {
			Sms sms = new Sms("http://jh92.jhmcc.com:9080/OpenMasService");
			
			String phones=new String((request.getParameter("phone")).getBytes("iso-8859-1"),"utf-8");
			//String[] destinationAddresses = new String[] { "15355425371" };
			String[] destinationAddresses =phones.split(",");
			//System.out.println(destinationAddresses);
			String message = new String((request.getParameter("address")).getBytes("iso-8859-1"),"utf-8");
			String extendCode = ""; 
			String ApplicationID = "8011002";
			String Password = "1dgNKxIQSt4h";
			
			String GateWayid = "";
				GateWayid = sms.SendMessage(destinationAddresses, message,
						extendCode, ApplicationID, Password);
				System.out.println(GateWayid);
	         if(GateWayid!="") {
	        	 return "second";
	         }else {
	        	 return "false";
	         }
			
		} catch (Exception ex) {
			System.err.println(ex);
			return "false";
		}
	}
	
	
	/**
	 * 发送彩信
	 * @param address 信息内容
	 * @param phone  号码组
	 * @param request 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("/sendMMS")
	public @ResponseBody String sendMMS( String address, String phone,HttpServletRequest request) throws UnsupportedEncodingException{
		try {
			 //彩信
			  Mms mms = new Mms("http://127.0.0.1:9090/openmasservice"); //彩信接口地址
			  String phones=new String((request.getParameter("phone")).getBytes("iso-8859-1"),"utf-8");
			  String[] destinationAddresses =phones.split(",");
			  //String[] destinationAddresses = new String[]{"13700000000"};//发送对象：11数字手机号码
			  String subject="彩信测试";//彩信标题
			  String extendCode = ""; //自定义扩展码（模块），可以为空，将显示在手机终端来信号码末位，注：来信号码总长度不能超过20位
			 // String ApplicationID= "default";//应用账号
			 // String Password = "default";//应用密码
			  String ApplicationID = "8011002";
			  String Password = "1dgNKxIQSt4h";
			  MmsContent mmsContent = MmsContent.CreateFromFile("1.gif");
			  String message = new String((request.getParameter("address")).getBytes("iso-8859-1"),"utf-8");
			  MmsBuilder body = new MmsBuilder();//构建彩信
			  body.AddContent(mmsContent);
			  mmsContent = MmsContent.CreateFromBytes(message.getBytes());
			  mmsContent.setCharset("gb2312");
			  mmsContent.setContentID("2");
			  mmsContent.setContentType(MmsConst.TEXT);
			  body.AddContent(mmsContent);
			  String content = body.BuildContentToXml();
		
			  String GateWayid ="";
			  //发送彩信 方法三
			  GateWayid = mms.SendMessage(destinationAddresses, subject,content, extendCode, ApplicationID, Password);
			  System.out.println(GateWayid);
			  if(GateWayid!="") {
		        	 return "second";
		         }else {
		        	 return "false";
		         }
			
		} catch (Exception ex) {
			System.err.println(ex);
			return "false";
		}
	}
	
	

	
	
}
